package com.feige.result;



import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.feige.constant.Constants;
import com.feige.enums.ResultEnumCode;
import com.feige.utils.string.StringUtils;

import java.util.HashMap;

/**
 * @author feige
 */
public class AjaxResult extends HashMap<String,Object> {

    public AjaxResult(int code,String msg,Object data){
        super.put(Constants.CODE_TAG,code);
        super.put(Constants.MSG_TAG,msg);
        super.put(Constants.DATA_TAG,data);
    }
    public AjaxResult(int code,String msg){
        super.put(Constants.CODE_TAG,code);
        super.put(Constants.MSG_TAG,msg);
    }

    public AjaxResult(ResultEnumCode resultEnumCode){
        super.put(Constants.CODE_TAG,resultEnumCode.getCode());
        super.put(Constants.MSG_TAG,resultEnumCode.getMsg());
    }
    public AjaxResult(){
    }

    /**
     * 分页
     * @param page
     * @return
     */
    public static AjaxResult page(IPage<?> page){
        AjaxResult ajaxResult = new AjaxResult(ResultEnumCode.success);
        if (ObjectUtil.isNotNull(page)){
            ajaxResult.put(Constants.DATA_TAG,page.getRecords());
            ajaxResult.put(Constants.PAGES_TAG,page.getPages());
            ajaxResult.put(Constants.TOTAL_TAG,page.getTotal());
        }
        return ajaxResult;
    }


    /**
     * 成功
     * @return
     */
    public static AjaxResult success(){
        return new AjaxResult(ResultEnumCode.success);
    }

    public static AjaxResult success(int code, String msg){
        return new AjaxResult(code,msg);
    }

    public static AjaxResult success(int code, String msg,Object data){
        return new AjaxResult(code,msg,data);
    }
    public static AjaxResult success(Object data){
        AjaxResult ajaxResult = new AjaxResult(ResultEnumCode.success);
        if (StringUtils.isNotNull(data)){
            ajaxResult.put(Constants.DATA_TAG,data);
        }
        return ajaxResult;
    }

    /**
     * 失败
     * @param data
     * @return
     */
    public static AjaxResult error(Object data){
        AjaxResult ajaxResult = new AjaxResult(ResultEnumCode.error);
        if (StringUtils.isNotNull(data)){
            ajaxResult.put(Constants.DATA_TAG,data);
        }
        return ajaxResult;
    }
    public static AjaxResult error(int code, String msg,Object data){
        return new AjaxResult(code,msg,data);
    }
    public static AjaxResult error(int code, String msg){
        return new AjaxResult(code,msg);
    }
    public static AjaxResult error(){
        return new AjaxResult(ResultEnumCode.error);
    }

    /**
     * @description: 判断增删改是否成功
     * @author: feige
     * @date: 2021/6/5 22:44
     * @param	flag
     * @return: com.feige.result.AjaxResult
     */
    public static AjaxResult isSuccess(boolean flag){
        if (flag){
            return success();
        }
        return error();
    }

    public <T> R<T> toR(){
        Integer code = (Integer) super.get(Constants.CODE_TAG);
        String msg = (String) super.get(Constants.MSG_TAG);
        Object o = super.get(Constants.DATA_TAG);
        return R.getInstance(code,msg,(T) o);
    }

}
